package com.xw.bo.ks.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * 自己写一写反射相关
 *
 * @author： XW -> ~
 * @create： 2023/12/9 10:06
 */
@Slf4j
public class ClassReflectionUtil {

    public static void findField(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            log.info("f:name={},typeName={},type={}", f.getName(), f.getType().getName(), f.getType());

        }
    }

    public static <T> void findField(String fieldName, T t) {
        if (t == null) {
            return;
        }
        Class<?> clazz = t.getClass();
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object value = field.get(t);
            log.info("value = {}", value.toString());
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        log.info("value = {}", 1);
//        KsGoodsSkuInfoVo infoVo = new KsGoodsSkuInfoVo();
//        infoVo.setId(12312);
//        ClassReflectionUtil.findField("id",infoVo);
//        ClassReflectionUtil.findField(KsGoodsSkuInfoVo.class);
    }

}
